const path = require('path')
const fs = require('fs')
// 图片验证
function imgVerifyFn(ctx, paths, fileType, file) {
  if (fileType.includes(file.type)) {
    const baseName = path.basename(file.path)
    let suffix = new RegExp(/(.[^\/]*)$/ig).exec(file.type)[0].replace('/', '')
    const avatarPath = path.resolve(paths, `./${baseName}.${suffix}`)
    const readStream = fs.createReadStream(file.path)
    const writeStream = fs.createWriteStream(avatarPath)
    readStream.pipe(writeStream)
    const url = `${ctx.origin}/uploads/user/avatar/${baseName}.${suffix}`
    return url
  } else {
    ctx.throw(415, '不支持该类型格式的图片')
  }
}

module.exports = {
  imgVerifyFn
}